﻿/*************************************************************
 * Software of blackcore
 *
 * CLRVersion:4.0.30319.296
 * MachineName:SPACE
 * Author:Andy Huang
 * Email:itblackhole@gmail.com
 * DateTime：6/5/2013 3:42:40 PM
 * Function：
 *************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Text.RegularExpressions;
using Sob.Machine.WebPageParser.Entity;
using Sob.Common;
using System.IO;

namespace Sob.Machine.WebPageParser
{
    public class MachineParser
    {
        #region ******************************Private Property******************************
        private List<Keyword> Keywords;
        private List<Domain> Domains;
        #endregion ******************************Private Property******************************


        #region ******************************Public Property******************************
        #endregion ******************************Public Property******************************


        #region ******************************Structure******************************
        private MachineParser()
        {

        }

        public MachineParser(List<Keyword> keywords, List<Domain> domains)
            : this()
        {
            this.Keywords = keywords;
            this.Domains = domains;
        }
        #endregion  ******************************Structure******************************


        #region ******************************Private Method******************************
        /// <summary>
        /// 获取页面中的链接
        /// </summary>
        /// <param name="htmlSource"></param>
        /// <returns></returns>
        private List<LinkItem> Find(string htmlSource)
        {
            List<LinkItem> list = new List<LinkItem>();

            MatchCollection mc = Regex.Matches(htmlSource, @"(<a.*?>.*?</a>)", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            foreach (Match m in mc)
            {
                string value = m.Groups[1].Value;
                LinkItem link = new LinkItem();

                // Get href attribute.
                Match mHref = Regex.Match(value, @"href=\""(.*?)\""", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                if (mHref.Success)
                {
                    link.Href = mHref.Groups[1].Value;
                }

                // Remove inner tags from text.
                string innerText = Regex.Replace(value, @"\s*<.*?>\s*", "", RegexOptions.Singleline);
                link.Text = innerText;

                list.Add(link);
            }
            return list;
        }


        #endregion ******************************Private Method******************************


        #region ******************************Public Method******************************
        public void Spider()
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();

            foreach (Domain domain in this.Domains)
            {
                if (domain == null) continue;

                if (string.IsNullOrWhiteSpace(domain.Url))
                {
                    Console.WriteLine("主域为空，无法进行操作！");
                    continue;
                }

                try
                {
                    Uri uri = new Uri(domain.Url);                   
                }
                catch (Exception err)
                {
                    Console.WriteLine(err.Message);
                }
            }

            sw.Stop();
        }
        #endregion ******************************Public Method******************************
    }
}
